#pragma once

#include <FreeImage.h>
#include "Camera.h"
#include "Mesh.h"
#include <array>
#include <string>

namespace soft_renderer
{
	typedef std::array<float, 3> Vec3f;
	typedef std::array<float, 2> Vec2f;
	typedef std::array<int, 2> Vec2i;

	class Matrix4;

	class Renderer
	{
	private:
		int width = 800;
		int height = 800;
		FIBITMAP* bitmap = nullptr;
		float* zbuffer = nullptr;

		Matrix4 _vertexMatrix;

		// viewport
		Matrix4 _viewportMatrix;

		// 质心坐标
		// A,B,C: 三角形三个顶点在屏幕上的坐标
		// P: 屏幕像素点
		// 返回值：质心坐标
		static Vec3f barycentric(Vec3f A, Vec3f B, Vec3f C, Vec3f P);
		static Vec3f cross(Vec3f a, Vec3f b);
		Vec3f world2screen(Vector3 p);

	public:
		Renderer(int w, int h);
		~Renderer();

		void saveAs(std::string filename);
		void render(const Camera& camera, Mesh& mesh);
		void triangle(Vector3 a, Vector3 b, Vector3 c, Vector3 na, Vector3 nb, Vector3 nc);
	};
}
